package com.junzhiit.nengji.api.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.base.spring.Context;
import com.junzhiit.nengji.api.service.VipService;
import com.junzhiit.nengji.api.web.exception.UserNotFoundException;
import com.junzhiit.nengji.api.web.exception.VipNotFoundException;
import com.junzhiit.nengji.api.web.util.Constants;
import com.junzhiit.nengji.api.web.util.RequestUtil;
import com.junzhiit.nengji.api.web.util.tools.NumberUtils;
import com.junzhiit.nengji.api.web.util.wechat.WeChatUtils;
import com.junzhiit.nengji.api.web.util.wechat.bean.OrderInfo;
import com.junzhiit.nengji.api.web.util.wechat.bean.PayParam;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.api.web.vo.VipAddVO;
import com.junzhiit.nengji.dao.SystemConfigDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.UserVipDAO;
import com.junzhiit.nengji.dao.VipDAO;
import com.junzhiit.nengji.model.SystemConfig;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.model.UserVip;
import com.junzhiit.nengji.model.Vip;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.UserVipType;

@Service
public class VipServiceImpl extends Context implements VipService {

	@Autowired
	private VipDAO vipDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private SystemConfigDAO systemConfigDAO;
	
	@Autowired
	private UserVipDAO userVipDAO;
	
	private final String WECHAT_BODY_KEY = "乐鸽订单";
	
	@Override
	public ResultVO getVipByIdentity(Integer identity) {
		ResultVO result = new ResultVO();
		result.setData(vipDAO.getVipByIdentity(identity));
		return result;
	}

	@Override
	public ResultVO payVip(VipAddVO vo,HttpServletRequest request) {
		ResultVO result = new ResultVO();
		User user = userDAO.query(vo.getUserId());
		if(user == null){
			throw new UserNotFoundException("用户找不到");
		}
		Vip vip = vipDAO.query(vo.getVipId());
		if(vip == null){
			throw new VipNotFoundException("找不到身份");
		}
		String number = NumberUtils.getOrderlNumber();
		String userVipId = null;
		UserVip checkVip = userVipDAO.checkUserByType(vo.getUserId(),vo.getVipId());
		if(checkVip == null){
			UserVip userVip = new UserVip();
			userVip.setUserId(vo.getUserId());
			userVip.setVipId(vo.getVipId());
			userVip.setIsUse(Is.NO.toIntValue());
			userVip.setType(UserVipType.BUY.toIntType());
			userVip.setNumber(number);
			userVip.setName(vo.getUserName());
			userVip.setMobilePhone(vo.getMobilePhone());
			userVip.setCityId(vo.getCityId());
			UserVip params = userVipDAO.insert(userVip);
			userVipId=params.getId();
		}else{
			userVipId = checkVip.getId();
			checkVip.setNumber(number);
			userVipDAO.update(checkVip);
		}
		SystemConfig systemConfig = systemConfigDAO.find();
		// 发起微信支付申请
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setAttach(Constants.WECHAT_ATTACH_FOR_VIP);
		orderInfo.setOutTradeNo(number);
		orderInfo.setBody(WECHAT_BODY_KEY +"-"+number);
		orderInfo.setTotalFee(Math.round(vo.getPayMoney() * 100));
		orderInfo.setSpbillCreateIp(RequestUtil.getRealIp(request));
		orderInfo.setOpenId(user.getOpenId());
		orderInfo.setTradeType(vo.getTradeType());
		PayParam payParam = WeChatUtils.getPayParam(orderInfo);
		Map<String, Object> map = new HashMap<String, Object>();
		if(systemConfig != null && systemConfig.getIsOpenKeyWord() == Is.YES.toIntValue()){
			map.put("keyWord", systemConfig.getKeyWord());
		}
		map.put("patParam", payParam);
		map.put("userVipId", userVipId);
		result.setData(map);
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		result.setData(userVipDAO.delete(id));
		return result;
	}

}